package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.common.goods.GoodsState;
import com.qf.entity.dto.Goods;
import com.qf.entity.dto.GoodsSku;
import com.qf.entity.po.LoginMerchant;
import com.qf.mapper.GoodsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class GoodsService extends ServiceImpl<GoodsMapper, Goods> {
    @Transactional
    public void upByGoodsId(Long goodsId) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Goods::getId, goodsId)
                .set(Goods::getStatus, GoodsState.UP.getCode());
        this.update(updateWrapper);
    }

    @Transactional
    public void downByGoodsId(Long goodsId) {
        UpdateWrapper<Goods> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Goods::getId, goodsId)
                .set(Goods::getStatus, GoodsState.DOWN.getCode());
        this.update(updateWrapper);
    }

    @Resource
    GoodsSkuService goodsSkuService;

    /**
     * 对商品表进行添加或修改
     * 对商品套餐表进行先删除再插入操作，实现套餐表的更新
     *
     * @param goods
     * @param goodsSku
     * @param loginMerchant
     */
    @Transactional
    public void saveOrUpdates(Goods goods, GoodsSku goodsSku, LoginMerchant loginMerchant) {
        goods.setMerchantId(loginMerchant.getId());
        goods.setStatus(GoodsState.WAIT_CHECK.getCode());
        this.saveOrUpdate(goods);
        if (goods.getId() != null) {
            //删除套餐表中该商品的套餐信息
            QueryWrapper<GoodsSku> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.lambda().eq(GoodsSku::getGoodsId, goods.getId());
            goodsSkuService.remove(deleteWrapper);
        }
        //添加该商品套餐信息
        Long orderNum = 1L;
        String[] titles = goodsSku.title.split("\\|");
        String[] costs = goodsSku.cost.split("\\|");
        String[] prices = goodsSku.price.split("\\|");
        String[] profits = goodsSku.profit.split("\\|");
        List<GoodsSku> skuList = new LinkedList<>();
        for (int i = 0; i < titles.length; i++) {
            if (!titles[i].equals("\\|")) {
                GoodsSku goodsSku1 = new GoodsSku();
                goodsSku1.setOrderNum(orderNum);
                goodsSku1.setTitle(titles[i]);
                goodsSku1.setCost(costs[i]);
                goodsSku1.setPrice(prices[i]);
                goodsSku1.setProfit(profits[i]);
                goodsSku1.setGoodsId(goods.getId());
                skuList.add(goodsSku1);
                orderNum++;
            }
        }
        goodsSkuService.saveBatch(skuList,skuList.size());
    }

    /**
     * 根据商品id删除该商品以及其套餐记录
     * @param goodsId
     */
    @Transactional
    public void removeByGoodsId(Long goodsId) {
        this.removeById(goodsId);
        QueryWrapper<GoodsSku> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.lambda().eq(GoodsSku::getGoodsId, goodsId);
        goodsSkuService.remove(deleteWrapper);
    }
}
